package org.example.enums;

import cn.hutool.core.util.ObjectUtil;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: sats@jz
 * @Date: 2025/1/11 22:33
 **/
@Getter
public enum ProtocolTypeEnum {
    HTTP(0, "http"),
    TCP(1, "tcp"),
    ;
    private int key;
    private String value;
    ProtocolTypeEnum(int key, String value) {
        this.key = key;
        this.value = value;
    }
    public static List<String> getValues() {
        return Arrays.stream(values()).map(item -> item.value).collect(Collectors.toList());
    }

    /**
     * 根据 key 获取枚举
     *
     * @param key
     * @return
     */
    public static ProtocolTypeEnum getEnumByKey(int key) {
        for (ProtocolTypeEnum anEnum : ProtocolTypeEnum.values()) {
            if (anEnum.key == key) {
                return anEnum;
            }
        }
        return null;
    }

    public static ProtocolTypeEnum getEnumByValue(String value) {
        if (ObjectUtil.isEmpty(value)){
            return  null;
        }

        for(ProtocolTypeEnum anEnum : ProtocolTypeEnum.values()){
            if(anEnum.value.equals(value)){
                return anEnum;
            }
        }

        return  null;
    }


}
